﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

using Axis.Dao.Research;
using Axis.BusinessObject.Research;
using Axis.Web.Administrator.Global;
using System.Web.Script.Serialization;
using Axis.BusinessObject.Core;
using System.Web.SessionState;
using Axis.Dao.Core;

namespace Axis.Web.Administrator.Views.Navigation.Service
{
    /// <summary>
    /// Summary description for NavigationService
    /// </summary>
    public class NavigationService : IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["Action"];
            string output = string.Empty;
            switch (action)
            {
                case "INSERT":
                case "UPDATE":
                case "DELETE":
                    output = UpdateProject(context);
                    break;
                default:
                    output = GetMemberList(context);
                    break;
            }
            context.Response.Write(output);
        }
        protected string UpdateProject(HttpContext context)
        {
            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);
            /* Action */
            CCoreDao cCoreDao = new CCoreDao();
            CApplicationMessage message = cCoreDao.ActionListData(inputValue);
            string output = new JavaScriptSerializer().Serialize(message);
            return output;
        }
        protected string GetMemberList(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];

            /* Get QueryString to xml */
            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);

            CCoreDao cCoreDao = new CCoreDao();
            IList<CListData> list = cCoreDao.GetListData(inputValue);
            int totalRecords = cCoreDao.LastOutputValue.TotalRows;
            int totalPages = cCoreDao.LastOutputValue.TotalPages;
            string output = BuildJQGridResults(list, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), totalPages);
            return output;
        }
        protected string BuildJQGridResults(IList<CListData> projects, int numberOfRows, int pageIndex, int totalPages)
        {
            if (projects != null && projects.Count > 0)
            {
                CGrid.JQGridResults result = new CGrid.JQGridResults();
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CListData item in projects)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = item.ID;
                    row.cell = new string[11];
                    row.cell[0] = item.ID.ToString();
                    row.cell[1] = item.Code;
                    row.cell[2] = item.Name;
                    row.cell[3] = item.ParentName;
                    row.cell[4] = item.ParentID.ToString();
                    row.cell[5] = item.Description;
                    row.cell[6] = item.ZOrder.ToString();
                    row.cell[7] = string.Format("<img  class='cell_image' src='{0}' FileName='{0}'/>", item.ActiveImage);
                    row.cell[8] = string.Format("<img  class='cell_image' src='{0}' FileName='{0}'/>", item.DeActiveImage);
                    row.cell[9] = string.Format("<img  class='cell_image' src='{0}' FileName='{0}'/>", item.Image);
                    row.cell[10] = string.Format("<div style='background:#{0};' value='{0}'>{0}</div>", item.BorderColor);
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
                return new JavaScriptSerializer().Serialize(result);
            }
            else
            {
                return string.Empty;
            }
        }
        protected string GetDateString(DateTime? dateTime)
        {
            string CreatedDateTime = dateTime.ToString();
            if (CreatedDateTime.Split(' ').Length > 0)
            {
                CreatedDateTime = CreatedDateTime.Split(' ')[0];
            }
            return CreatedDateTime;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}